﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Xml.Xsl;

/// <summary>
/// Summary description for XMLHelper
/// </summary>
/// <summary>
/// Helper Class for XML and XSL
/// </summary>
public class XMLHelper
{    

    #region CreateXMLAttribute
    /// <summary>
    /// Creates and returns an XML attribute
    /// </summary>
    /// <param name="sourceDoc">XMLDocument for which attribute is created</param>
    /// <param name="name">Attribute Name</param>
    /// <param name="value">Attribute Value</param>
    /// <returns>XML Attribute</returns>
    public static XmlAttribute CreateXMLAttribute(XmlDocument sourceDoc, string name, string value)
    {
        XmlAttribute attrib = sourceDoc.CreateAttribute(name);
        attrib.Value = value;
        return attrib;
    }

    /// <summary>
    /// Creates and returns an XML attribute
    /// </summary>
    /// <param name="sourceDoc">XMLDocument for which attribute is created</param>
    /// <param name="name">Attribute Name</param>
    /// <param name="value">Attribute Value</param>
    /// <returns>XML Attribute</returns>
    public static XmlAttribute AddAttribute(XmlNode currentNode, string name, string value)
    {
        XmlAttribute attrib = null;
        if (currentNode != null)
        {
            attrib = currentNode.OwnerDocument.CreateAttribute(name);
            attrib.Value = value;
            currentNode.Attributes.Append(attrib);
        }

        return attrib;
    }

    public static XmlNode AddNode(XmlNode parentNode, string nodeName, string nodeText, string nodeXML)
    {
        if (parentNode != null)
        {
            XmlNode node = (XmlNode)parentNode.OwnerDocument.CreateElement(nodeName);
            if (!string.IsNullOrEmpty(nodeText))
            {
                node.InnerText = nodeText;
            }

            if (!string.IsNullOrEmpty(nodeXML))
            {
                node.InnerXml = nodeXML;
            }

            parentNode.AppendChild(node);
            return node;
        }
        return parentNode;
    }

    public static void RemoveNode(XmlNode parentNode, string xpath)
    {
        if (parentNode != null)
        {
            XmlNodeList nodeL = parentNode.SelectNodes(xpath);
            if (!(nodeL == null || nodeL.Count == 0))
            {
                foreach (XmlNode node in nodeL)
                {
                    node.ParentNode.RemoveChild(node);
                }
            }
        }
    }
    #endregion CreateXMLAttribute

    #region Public Static Methods
    /// <summary>
    /// Get InnerText of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static string GetInnerText(System.Xml.XmlNode Node)
    {
        if (Node != null)
        {
            return Node.InnerText;
        }
        return string.Empty;
    }

    /// <summary>
    /// Get InnerText of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static string GetInnerXml(System.Xml.XmlNode Node)
    {
        if (Node != null)
        {
            return Node.InnerXml;
        }
        return string.Empty;
    }

    /// <summary>
    /// Get InnerXml of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static string GetInnerXml(XmlDocument DocXML, string XPATH)
    {
        if (!(DocXML == null || DocXML.DocumentElement == null))
        {
            return GetInnerXml(DocXML.SelectSingleNode(XPATH));
        }
        return string.Empty;
    }

    /// <summary>
    /// Get InnerText of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static string GetInnerText(XmlDocument DocXML, string XPATH)
    {
        if (!(DocXML == null || DocXML.DocumentElement == null))
        {
            return GetInnerText(DocXML.SelectSingleNode(XPATH));
        }
        return string.Empty;
    }


    /// <summary>
    /// Get InnerText of a parent Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static string GetInnerText(XmlNode Node, string XPATH)
    {
        if (!(Node == null))
        {
            return GetInnerText(Node.SelectSingleNode(XPATH));
        }
        return string.Empty;
    }


    /// <summary>
    /// Get InnerText as a int of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static int GetInnerTextInt(System.Xml.XmlNode Node)
    {
        int data = 0;
        int.TryParse(GetInnerText(Node), out data);
        return data;
    }

    /// <summary>
    /// Get InnerText as a int of a Xml Document 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static int GetInnerTextInt(XmlDocument DocXML, string XPATH)
    {
        int data = 0;
        int.TryParse(GetInnerText(DocXML, XPATH), out data);
        return data;
    }


    /// <summary>
    /// Get InnerText as a int of a Xml Node 
    /// </summary>
    /// <param name="Node"></param>
    /// <returns></returns>
    public static int GetInnerTextInt(XmlNode XMLNode, string XPATH)
    {
        int data = 0;
        int.TryParse(GetInnerText(XMLNode, XPATH), out data);
        return data;
    }

    /// <summary>
    /// Get InnerText as a long of a Xml Node 
    /// </summary>
    /// <param name="Node">XML Node</param>
    /// <returns>long</returns>
    public static long GetInnerTextLong(System.Xml.XmlNode Node)
    {
        long data = 0;
        long.TryParse(GetInnerText(Node), out data);
        return data;
    }

    /// <summary>
    /// Get InnerText as a long of a parent Xml Node 
    /// </summary>
    /// <param name="Node">XML Node</param>
    /// <returns>long</returns>
    public static long GetInnerTextLong(System.Xml.XmlNode Node, string XPATH)
    {
        long data = 0;
        XmlNode xnode = Node.SelectSingleNode(XPATH);
        long.TryParse(GetInnerText(xnode), out data);
        return data;
    }

    /// <summary>
    /// Get InnerText as a long of a Xml Node 
    /// </summary>
    /// <param name="Node">XML Node</param>
    /// <returns>long</returns>
    public static long GetInnerTextLong(XmlDocument DocXML, string XPATH)
    {
        long data = 0;
        long.TryParse(GetInnerText(DocXML, XPATH), out data);
        return data;

    }


    public static DateTime? GetInnerTextNullableDateTime(XmlDocument DocXML, string XPATH)
    {
        string text = GetInnerText(DocXML, XPATH);
        DateTime dt;
        try
        {
            dt = DateTime.Parse(text);
            return dt;
        }
        catch
        {
            return null;
        }
    }

    #endregion Public Static Methods
}

